<template>
  <div>
    <el-form ref="form" v-loading="loading" :model="entity" :rules="rules" label-width="80px">
      <el-form-item label="ID" prop="id">
        <el-input v-model="entity.id" disabled></el-input>
      </el-form-item>
      <el-form-item label="编码" prop="code">
        <el-input v-model="entity.code"></el-input>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="entity.name"></el-input>
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="entity.type">
          <el-option v-for="(item, index) in dictionary.TagType" :key="index" :label="item"
                     :value="parseInt(index)"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSave()">保存</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="success" @click="handleSaveAndNext()">保存并添加下一个</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { mapGetters } from 'vuex';
// import TagApi from '@/api/mathsgo/tag'

export default {
    name: 'tag-edit',
    props: {
        id: {
            type: String,
            default: ''
        }
    },
    data: function() {
        return {
            loading: 0,
            entity: {
                id: null,
                name: null,
                code: null,
                type: null
            },
            rules: {
                name: {
                    required: true, trigger: 'blur'
                },
                type: {
                    required: true, trigger: 'blur'
                }
            }
        };
    },
    computed: {
        ...mapGetters([
            'dictionary'
        ])
    },
    created: function() {
        this.init();
    },
    methods: {
        init(id) {
            if (id) {
                this.getEntity(id);
            } else {
                this.entity = Object.assign({}, this.newEntity());
            }
        },
        newEntity() {
            return {
                id: null,
                editorId: null,
                editorName: null,
                deadline: null,
                // EditorTaskStatus
                status: 2
            };
        },
        getEntity(id) {
            this.loading++;
        // TagApi.getEntity(id)
        //   .then(data => {
        //     this.loading--
        //     this.entity = Object.assign(this.entity, data.obj)
        //   })
        },
        handleSave(callback) {
            this.$refs['form'].validate((valid) => {
                if (!valid) {
                    this.$message({
                        type: 'warning',
                        message: '格式有误!'
                    });
                    return;
                }
                this.loading++;
                // TagApi.save(this.entity)
                //   .then(data => {
                //     this.loading--
                //     this.entity = Object.assign(this.entity, data.obj)
                //     this.$message({
                //       type: ' success',
                //       message: '保存成功'
                //     })
                //     this.$emit('saved')
                //     callback && callback()
                //   })
                //   .catch(() => {
                //     this.loading--
                //   })
            });
        },
        handleSaveAndNext() {
            this.handleSave(() => {
                this.entity = Object.assign(this.newEntity(), {
                    type: this.entity.type
                });
            });
        }
    }
};
</script>

<style>

</style>
